var WebSocket = require('ws');
/* {
	from: 'ybf',
	to: 'll',
	data: 'something'
}
 */
var wss = new WebSocket.Server({
	port: 8083
});
var clientArr = {};
wss.on('connection', function (ws,req) {
	clientArr[req.url.replace('/','')] = ws;
  ws.on('message', function (message,req) {
		message = JSON.parse(message);
		console.log(message);
		if(clientArr[message.to]){
			if(clientArr[message.to].readyState == WebSocket.OPEN){
				clientArr[message.to].send(message.data);
			}else{
				delete clientArr[message.to];
			}
		}else{
			ws.send('对方不在线');
		}
	});
	//服务端查看在先状态
	if(req.url.replace('/','') == 'admin'){
		var userArrStatus = setInterval(function(){
			if(clientArr['admin'].readyState == WebSocket.OPEN){
				clientArr['admin'].send(JSON.stringify(Object.keys(clientArr)));
			}else{
				delete clientArr['admin'];
				clearInterval(userArrStatus);
			}
		},1000);
	}
});